package com.ybb.vue.controller;

import com.ybb.vue.common.Result;
import com.ybb.vue.domain.AlarmMessageEnum;
import com.ybb.vue.domain.AlarmTypeEnum;
import com.ybb.vue.entity.Message;
import com.ybb.vue.mapper.MessageMapper;
import com.ybb.vue.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ybb
 * @version 1.0
 * @Description TODO
 * @Date 2023/4/2 21:14
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageMapper messageMapper;

    /**
     * message
     *
     * @return
     */
    @GetMapping("/allMessage/{username}")
    public Result<?> message(@PathVariable("username") String username) {
        List<Message> list = messageMapper.selectAllMessage(username);
        list.forEach(message -> {
            // 告警类型转译
            message.setAlarmType(AlarmTypeEnum.getAlarmType(message.getAlarmType()));
            // 消息状态转译
            message.setStatus(AlarmMessageEnum.getAlarmMessage(message.getStatus()));
        });
        return Result.success(list);
    }

    /**
     * 改变消息状态
     *
     * @return
     */
    @GetMapping("/changeMsgStatus/{id}")
    public Result<?> changeMsgStatus(@PathVariable("id") String id) {
        Message message = messageMapper.selectById(id);
        message.setStatus("1");
        messageMapper.updateById(message);
        return Result.success();
    }


    /**
     * 消息详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result<?> detail(@PathVariable("id") String id) {
        Message message = messageMapper.selectById(id);
        // 告警类型转译
        message.setAlarmType(AlarmTypeEnum.getAlarmType(message.getAlarmType()));
        // 消息状态转译
        message.setStatus(AlarmMessageEnum.getAlarmMessage(message.getStatus()));
        return Result.success(message);
    }
}
